package net.qktianxia.component.logger;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * A proxy interface to enable additional operations.
 * Contains all possible Log message usages.
 */
public abstract class IPrinter {


    private final List<ILogProxy> logProxyList = new ArrayList<>();

    public void addLogProxy(@NonNull ILogProxy logProxy) {
        logProxyList.add(logProxy);
    };

    public List<ILogProxy> getLogProxyList() {
        return logProxyList;
    }

    public abstract IPrinter t(@Nullable String tag);

    public abstract void d(@NonNull String message, @Nullable Object... args);

    public abstract void d(@Nullable Object object);

    public abstract void e(@NonNull String message, @Nullable Object... args);

    public abstract void e(@Nullable Throwable throwable, @NonNull String message, @Nullable Object... args);

    public abstract void w(@NonNull String message, @Nullable Object... args);

    public abstract void i(@NonNull String message, @Nullable Object... args);

    public abstract void v(@NonNull String message, @Nullable Object... args);

    public abstract void wtf(@NonNull String message, @Nullable Object... args);

    /**
     * Formats the given json content and print it
     */
    public abstract void json(@Nullable String json);

    /**
     * Formats the given xml content and print it
     */
    public abstract void xml(@Nullable String xml);


    public void clearLogProxyList() {
        logProxyList.clear();
    };


    public final synchronized void log(int priority,
                                       @Nullable String tag,
                                       @Nullable String message,
                                       @Nullable Throwable throwable) {
        if (throwable != null && message != null) {
            message += " : " + Utils.getStackTraceString(throwable);
        }
        if (throwable != null && message == null) {
            message = Utils.getStackTraceString(throwable);
        }
        if (Utils.isEmpty(message)) {
            message = "Empty/NULL log message";
        }

        for (final ILogProxy adapter : logProxyList) {
            fireLogAdapter(priority, tag,message,adapter);
        }
    }

    private void fireLogAdapter(int priority,
                                @Nullable String tag,
                                @Nullable String message,
                                ILogProxy adapter){

        if(!adapter.isLoggable(priority,tag)){
            return;
        }
        adapter.log(priority, tag, message);
    }

}
